//checkbox计数器
var qCheckboxCounter=0;

//构造品牌html 供asmSelect 调用方法addListItem的时候使用
function externalBrandHtml(value,externalValue){
	return buildHiddenBrandIdHTML(value);
}
//生成hidden 关联品牌html
function buildHiddenBrandIdHTML(value){
	var html = '';
	html += '<span style="display:none"><input type="hidden" id="relatedBrandIds" name="relatedBrandIds" value="'+value+'"/></span>';
	return html;
}

//构造规格html 供asmSelect 调用方法addListItem的时候使用
function externalSkuOptionHtml(value,externalValue){
	return buildHiddenSkuOptionIdHTML(value)+buildQuickSearchHTML(externalValue);
}
//生成hidden 关联品牌html
function buildHiddenSkuOptionIdHTML(value){
	var html = '';
	html += '<span style="display:none"><input type="hidden" id="relatedSkuOptionIds" name="relatedSkuOptionIds" value="'+value+'"/></span>';
	return html;
}
//生成开启快速搜索选项html
function buildQuickSearchHTML(isQuickSearch){
	qCheckboxCounter++;
	var html = '';
	html += '<span class="asmListItemOpenClickSearch">'+__FMT.productTypeDetail_isQucikSearch;
	html += '<input type="checkbox" '+(isQuickSearch=="1"?'checked="checked"':'')+'name="isQuickSearchsCheck" id="statusCheck" onchange="changeQuickSearchCheck(this,'+qCheckboxCounter+')">';
	html += '<input type="hidden" name="isQuickSearch" id="isQuickSearch'+qCheckboxCounter+'" value="'+isQuickSearch+'">';
	html += '</span>';
	return html;
}

//改变开启快速搜索checkbox值
function changeQuickSearchCheck(checkbox,fieldId){
	if(checkbox.checked){
		$j("#isQuickSearch"+fieldId).val(1);
	}else{
		$j("#isQuickSearch"+fieldId).val(0);
	}
}


var deleteAttributeIdString = "";//用于保存删除的attributeId,例如 26,45,46,
var groupCounter =0;
//增加属性组
// tableDivId 是category所对应的attributeTable
// group 是一个productAttGroup的JSON对象{id,name,sortOrder}
function addGroup(tableDivId,group){
	var id,name,sortOrder;
	if(null == group){
		groupCounter++;
		id = "new_"+groupCounter;//包含表示新增
		name = "";
		var k = $j('#'+tableDivId+' > tr');
		sortOrder = $j('#'+tableDivId+' .groupClass').size()+1;//根据当前group个数决定排序值
	}else{
		id = "_"+group.id;
		name = group.name;
		sortOrder = group.sortOrder;
	}
	
	var html = '';
	html += '<li class="groupClass" id="group'+id+'" style="width:585px;">';
	html += '<div class="top">';
	html += '<span title="'+__FMT.productTypeDetail_attributeGroup_sortable+'">'+__FMT.productTypeDetail_attribute_groupName+'</span>';
	html += '<span><input class="inputtxt" type="text" style="width: 130px;" id="groupName'+id+'" name="groupNames" value="'+name+'" /></span>';
	html += '<span><a class="del" href="javascript:void(0)" onclick="delGroup(\''+tableDivId+'\',\'group'+id+'\')"><b>&nbsp;</b></a></span>';
	html += '</div>';
	
	html += '<div class="content"><div class="item">';
	html += '<table  id="attrItemTable'+id+'" class="property_table" cellspacing="0" cellpadding="0" border="0" width="100%">';
	html += '<tr><th width="360">'+__FMT.productTypeDetail_attribute_attribute+'</th><th>'+__FMT.productTypeDetail_attribute_operation+'</th></tr>';
	html += '<tr class="bt"><td colspan="2"><input class="add_property_btn" type="button" name="" onclick="addAttribute(\'attrItemTable'+id+'\')"/></td></tr>';
	html += '</table>';
	html += '</div></div>';
	
	//start:hidden
	html += '<input type="hidden" name="groupIds" value="groupId'+id+'" id="groupId'+id+'"/>';
	//end:hidden
	
	html += '</li>';
	
	$j('#'+tableDivId).append(html);
	
	return "attrItemTable"+id;
}

//删除group
// tableDivId 是category所对应的attributeTable
// trDivId 是group所对应的groupTr;
function delGroup(tableDivId,trDivId){
	if(confirm(__FMT.productTypeDetail_attribute_deleteGroup_tip)){
		//保存删除的属性id
		var attributeIds = $j('#'+trDivId+' input[name="aiAttributeIds"]');
		attributeIds.each(function(){
			deleteAttributeIdString += this.value+",";
		});
		$j('#'+trDivId).remove();
		
	}
}


var attrItemCounter = 0;
//增加attrItem
// tableDivId 是group所对应的attrItemTable;
// attrItem是一个productAttrItem JSON对象{attrItemId,attributeId,sortOrder,isQuickSearch,showStyle,searchValue,attribute}; 其中attribute是一个attribute的JSON对象{attributeId,attributeName,attributeDataType,attributeType,isRequired,status}
function addAttrItem(tableDivId,attrItem){
	var id,groupId,attributeId,sortOrder,isQuickSearch,showStyle,searchValue;
	if(null == attrItem.attrItemId){
		attrItemCounter++;
		id = "new_"+attrItemCounter;//包含表示新增
		sortOrder = $j('#'+tableDivId+' .attrItemClass').size()+1; //根据当前group内attrItem个数决定sortOrder
	}else{
		id = "_"+attrItem.attrItemId;
		sortOrder = attrItem.sortOrder;
	}
	attributeId = attrItem.attributeId;
	isQuickSearch = attrItem.isQuickSearch;
	showStyle = attrItem.showStyle;
	searchValue = attrItem.searchValue;
	var groupTr = $j('#'+tableDivId).parents(".groupClass");
	var groupIdInput = $j('input[name="groupIds"]',groupTr);
	groupId = "ai"+groupIdInput.val();
	var html = "";
	
	html += '<tr class="attrItemClass" id="attrItem'+id+'">';
	html += '<td  id="attributeName'+attributeId+'">'+attrItem.attribute.attributeName+'</td>';
	html += '<td>';
	html += '<a  href="javascript:void(0)" onclick="editAttribute(\''+tableDivId+'\',\''+attributeId+'\')" class="edit"></a>';
	html += '<a href="javascript:void(0)" onclick="moveUpAttribute(this)" name="move_up" class="up"></a>';
	html += '<a href="javascript:void(0)" onclick="moveDownAttribute(this)" name="move_down" class="down"></a>';
	html += '<a href="javascript:void(0)" onclick="deleteAttrItem(\''+tableDivId+'\',\''+id+'\')" class="del"></a>';
	html += '</td>';
	//start:hidden
	html += '<input type="hidden" name="aiIds" id="aiId'+id+'" value="aiId'+id+'"/>';
	html += '<input type="hidden" name="aiGroupIds" id="'+groupId+'" value="'+groupId+'"/>';
	html += '<input type="hidden" name="aiAttributeIds" id="aiAttributeId_'+attributeId+'" value="'+attributeId+'"/>';
	//isQuickSearch、showStyle和searchValue的hidden input的id用attributeId来命名，方便定位
	html += '<input type="hidden" name="aiIsQuickSearchs" value="'+isQuickSearch+'" id="isQuickSearch_'+attributeId+'"/>';
	html += '<input type="hidden" name="aiShowStyles" value="'+showStyle+'" id="showStyle_'+attributeId+'"/>';
	html += '<input type="hidden" name="aiSearchValues" value="'+searchValue+'" id="searchValue_'+attributeId+'"/>';
	//end:hidden
	html += '</tr>';
	
	
	$j('#'+tableDivId).append(html); 
	
	showOptionValueMoveIcons($j('#attrItem'+id));
	
}
//提升属性顺序
//linkDOM 是一个超链接对象
function moveUpAttribute(linkDOM){
	var me = $j(linkDOM).parents(".attrItemClass");
	var prev = me.prev(".attrItemClass");
	prev.before(me);
	showOptionValueMoveIcons(me);
}

//降低属性顺序
//linkDOM 是一个超链接对象
function moveDownAttribute(linkDOM){
	var me = $j(linkDOM).parents(".attrItemClass");
	var next = me.next(".attrItemClass");
	next.after(me);
	showOptionValueMoveIcons(me);
}

//属性上下移动按钮显示 jTr是属性所在的行的jquery对象
function showOptionValueMoveIcons(jTr){
	jTr.parent().find('.attrItemClass a[name="move_up"]').addClass("up").removeClass("up_gray");
	jTr.parent().find('.attrItemClass a[name="move_down"]').addClass("down").removeClass("down_gray");
	jTr.parent().find('.attrItemClass:first a[name="move_up"]').addClass("up_gray").removeClass("up");
	jTr.parent().find('.attrItemClass:last a[name="move_down"]').addClass("down_gray").removeClass("down");
}

var currTableDivId ; //用于记录当前增加属性的是哪个属性组
var currAttributeId ; //用于记录当前编辑的是哪个属性
//增加attribute
function addAttribute(tableDivId){
	currSearchValue = "";
	var url = __ctxPath+"/product/productType.html?decorator=windowPage&doAction=attributeDefine";
	openAttributeDefineDialog(url);
	//debug和测试可用下面方式
	//newWindow(url,650,1100);
	currTableDivId = tableDivId;
}
//增加attribute Callback quickSearchOption是attrItem中用于搜索的三个属性{isQuickSearch,showStyle,searchValue}
function addAttributeCallback(attribute,quickSearchOption){
	attrItem= {'attrItemId':null,'attributeId':attribute.attributeId,'sortOrder':null,'isQuickSearch':quickSearchOption.isQuickSearch,'showStyle':quickSearchOption.showStyle,'searchValue':quickSearchOption.searchValue,'attribute':attribute};
	addAttrItem(currTableDivId,attrItem);
	currSearchValue = "";
}
//编辑attribute 
function editAttribute(tableDivId,attributeId){
	var isQuickSearch = $('isQuickSearch_'+attributeId).value;
	var showStyle = $('showStyle_'+attributeId).value;
	var searchValue = $('searchValue_'+attributeId).value;
	var searchOptionString = "isQuickSearch="+isQuickSearch;
	searchOptionString += "&showStyle="+showStyle;
	searchOptionString += "&searchValue="+searchValue;
	var url = __ctxPath+"/product/productType.html?decorator=windowPage&doAction=attributeDefine&attributeId="+attributeId+"&"+searchOptionString;
	openAttributeDefineDialog(url);
	//debug和测试可用下面方式
	//newWindow(url,650,1100);
	currAttributeId = attributeId;
	currSearchValue = searchValue;
}
//临时保存searchValue 因为用url get 方法会出现中文乱码
var currSearchValue = ""; 
function getSearchValue(){
	return currSearchValue;
}
//编辑attribute  Callback
function editAttributeCallback(attribute,quickSearchOption){
	$j('#attributeName'+currAttributeId).html(attribute.attributeName);
	var attributeId = attribute.attributeId;
	$('isQuickSearch_'+attributeId).value = quickSearchOption.isQuickSearch;
	$('showStyle_'+attributeId).value = quickSearchOption.showStyle;
	$('searchValue_'+attributeId).value = quickSearchOption.searchValue;
}
//删除attrItem
//tableDivId 是group所对应的attrItemTable; id 是attrItem id 
function deleteAttrItem(tableDivId,id){
	if(confirm(__FMT.productTypeDetail_attribute_delete_tip)){
		var attributeId = $j('#attrItem'+id+' input[name="aiAttributeIds"]').get(0);
		deleteAttributeIdString += attributeId.value+","; //记录删除的属性
		$j('#attrItem'+id).remove();
	}	
}
//打开属性定义dialog
function openAttributeDefineDialog(url){
	dlgattributeDefineDialog_show();
	$j('#attributeDefineIframe').hide();
	$j(ui_dialog_attributeDefineDialog).loading();
	$j('#attributeDefineIframe').src(url,function(duration){$j('#attributeDefineIframe').show();$j(ui_dialog_attributeDefineDialog).unloading();});
}
//关闭属性定义dialog
function closeAttributeDefineDialog(){
	dlgattributeDefineDialog_close();
	$j('#attributeDefineIframe').get(0).src = "";
}


//新建window
function newWindow(url,height,width){
	//settings=settings.replace(/\s*/g,"").toLowerCase();
	var LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
	var winName=new Date().getTime().toString().substr(6);
	var settings ='height='+height+',width='+width+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,resizable=yes,location=no,toolbar=yes';
	var win = window.open(url,winName,settings);
	win.focus();
}

//保存前函数
function preSave(){
	
	if(validateAction()){
		fnAddHiddenField("deleteAttributeIdString",deleteAttributeIdString,$j('#productTypeForm').get(0));
		return true;
	}
	return false;
		
}

//验证
function validateAction(){
	
	$j('input[name="groupNames"]').each(function(index,obj){
		applyValidate(obj,"required,maxlength=25");
	});
	
	$j('input[name="groupSortOrders"]').each(function(index,obj){
		applyValidate(obj,"required,integer,minValue=1");
	});
	
	$j('input[name="aiSortOrders"]').each(function(index,obj){
		applyValidate(obj,"required,integer,minValue=1");
	});
	
	
	//validate the form	  	  
    if(!validateProductType($j("#productTypeForm").get(0)) ){
    	return false;
   	}
   
    return true;
    
}

//确认是否添加新品牌
function defineNewBrand(){
	if(confirm(__FMT.productType_confirmDefineAction)){
		window.location = __ctxPath+"/product/brand.html?doAction=add";
	}
}

//确认是否添加新品牌
function defineNewSkuOption(){
	if(confirm(__FMT.productType_confirmDefineAction)){
		window.location = __ctxPath+"/product/skuOption.html?doAction=add";
	}
}
//移除规格项li是一个jquery对象
function externalSkuOptionRemoveFunc(jLi){
	if(!confirm(__FMT.productType_removeSkuOption_confirm)){
		return false;
	}
	var productTypeId = $j('#productTypeId').val();
	if(productTypeId != ""){
		var skuOptionId = jLi.find('input[name="relatedSkuOptionIds"]').val();
		var postData = {"skuOptionId":skuOptionId,"productTypeId":productTypeId};
		var html = $j.ajax({
			async: false,
			url: __ctxPath+'/product/productType.html?doAction=removeSkuOptionCheck',
			data:postData
		}).responseText;
		if(html.indexOf("result=1")!= -1){
			//可删除
			return true;
		}else{
			//不可删除
			jAlert(__FMT.productType_removeSkuOption_false);
			return false;
		}	
	}else{
		//新类型一定可以移除
		return true;
	}	
}

//移除关联品牌
function externalBrandRemoveFunc(jLi){
	if(!confirm(__FMT.productType_removeBrand_confirm)){
		return false;
	}else{
		return true;
	}
}